package com.snow.useviewdemo.util.db;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created by zhaoyingtao
 * Date: 2020/7/5
 * Describe:应用统一SharedPreferences封装
 */
public class AppSPreferencesHelper {
    private static SharedPreferences sharedPreferences;
    private volatile static AppSPreferencesHelper appSPreferencesHelper;
    private static String SHARENAME = "";
    private static Context mContext;

    public static AppSPreferencesHelper init() {
        if (appSPreferencesHelper == null) {
            synchronized (AppSPreferencesHelper.class) {
                if (appSPreferencesHelper == null) {
                    appSPreferencesHelper = new AppSPreferencesHelper();
                }
            }
        }
        return appSPreferencesHelper;
    }

    /**
     * 在APPlication中初始化
     *
     * @param context
     * @param shareName
     */
    public void initDB(Context context, String shareName) {
        if (context == null || TextUtils.isEmpty(shareName)) {
            throw new NullPointerException("context or shareName not null");
        }
        mContext = context;
        SHARENAME = shareName;
        sharedPreferences = context.getSharedPreferences(SHARENAME, Context.MODE_PRIVATE);
    }

    /******************向数据库的存值***********************/
    public void putString(String key, String value) {
        if (sharedPreferences == null) {
            sharedPreferences = mContext.getSharedPreferences(SHARENAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void putBoolean(String key, boolean value) {
        if (sharedPreferences == null) {
            sharedPreferences = mContext.getSharedPreferences(SHARENAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public void putInt(String key, int value) {
        if (sharedPreferences == null) {
            sharedPreferences = mContext.getSharedPreferences(SHARENAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void putFloat(String key, float value) {
        if (sharedPreferences == null) {
            sharedPreferences = mContext.getSharedPreferences(SHARENAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public void putLong(String key, long value) {
        if (sharedPreferences == null) {
            sharedPreferences = mContext.getSharedPreferences(SHARENAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    /******************取出数据库的值***********************/
    public String getString(String key) {
        if (sharedPreferences != null) {
            return sharedPreferences.getString(key, "");
        }
        return "";
    }


    public int getInt(String key) {
        return getInt(key, -1);
    }

    public int getInt(String key, int defaultValue) {
        if (sharedPreferences != null) {
            return sharedPreferences.getInt(key, defaultValue);
        }
        return defaultValue;
    }

    public long getLong(String key) {
        return getLong(key, -1l);
    }

    public long getLong(String key, long defaultValue) {
        if (sharedPreferences != null) {
            return sharedPreferences.getLong(key, defaultValue);
        }
        return defaultValue;
    }

    public float getFloat(String key) {
        return getFloat(key, -1f);
    }

    public float getFloat(String key, float defaultValue) {
        if (sharedPreferences != null) {
            return sharedPreferences.getFloat(key, defaultValue);
        }
        return defaultValue;
    }

    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        if (sharedPreferences != null) {
            return sharedPreferences.getBoolean(key, defaultValue);
        }
        return false;
    }


    public void del(String key) {
        if (sharedPreferences != null) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove(key);
            editor.commit();
        }
    }
}
